package com.yanqu.road.server.cmd.orewar;

import com.yanqu.road.entity.activity.ActivityConditionInfo;
import com.yanqu.road.pb.cross.activity.CrossActivityProto;
import com.yanqu.road.server.cmd.CrossCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.logic.MessageHelper;
import com.yanqu.road.server.manager.orewar.CrossOreWarActivityMgr;
import com.yanqu.road.server.manager.orewar.CrossOreWarRankMgr;
import com.yanqu.road.utils.YanQuMessageUtils;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.Protocol;

@Cmd(code = Protocol.C_CROSS_OREWAR_GET_RANK_REWARD, desc = "领取跨服盐场排行奖励")
public class CrossGetRankRewardCmd extends CrossCommand {
    private int activityId;
    private int conditionId;
    private long userId;
    private String unionUid;
    private int position;

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        CrossActivityProto.CrossActivityGetRewardReqMsg reqMsg = CrossActivityProto.CrossActivityGetRewardReqMsg.parseFrom(packet.getBytes());
        activityId = reqMsg.getActivityId();
        conditionId = reqMsg.getConditionId();
        unionUid = reqMsg.getUnionUid();
        userId = packet.getPlayerId();
        position = reqMsg.getPosition();
        return true;
    }

    @Override
    public void execute(YanQuMessage packet) throws Exception {
        int myRank = -1;
        ActivityConditionInfo activityConditionInfo = CrossOreWarActivityMgr.getActivityConditionInfo(activityId, conditionId);
        if(null != activityConditionInfo){
            if(activityConditionInfo.getType() == CrossOreWarActivityMgr.CONDITION_TYPE_USER) {
                myRank = CrossOreWarRankMgr.getCrossActivityRank(activityId, serverId, userId);
            }else {
                myRank = CrossOreWarRankMgr.getCrossActivityUnionRank(activityId, serverId, unionUid);
            }
        }
        CrossActivityProto.CrossActivityGetRewardRespMsg.Builder respMsg = CrossActivityProto.CrossActivityGetRewardRespMsg.newBuilder();
        respMsg.setActivityId(activityId);
        respMsg.setConditionId(conditionId);
        respMsg.setRank(myRank);
        respMsg.setPosition(position);
        YanQuMessage pbMsg = YanQuMessageUtils.buildMessage(Protocol.S_CROSS_ORE_WAR_GET_RANK_REWARD_NOTIFY, respMsg);
        MessageHelper.sendPacket(serverId, userId, pbMsg);
    }

    @Override
    public void sendData() {

    }
}
